//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import org.json.JSONObject;

/**
 * @author liaoyong
 * @desc app更新model
 * @date 2016/4/11 13:57
 */
public class AppInfo implements Parcelable {
    public boolean isNewVersion;
    public boolean isForcedUpdate;
    public int versionCode;
    public String versioName;
    public boolean isPatchUpdate;
    public long size;
    public long patchUpdateSize;
    public String updateTitle;
    public String updateComment;
    public String downloadUrl;
    public String packageName;
    public String appStoreDownloadUrl;
    public String appstoreChannelName;
    public boolean enableUpdateByAppstore;
    public static final Creator<AppInfo> CREATOR = new Creator() {
        public AppInfo createFromParcel(Parcel source) {
            return new AppInfo(source);
        }

        public AppInfo[] newArray(int size) {
            return new AppInfo[size];
        }
    };

    AppInfo() {
        this.isNewVersion = false;
        this.isForcedUpdate = false;
        this.versionCode = 0;
        this.isPatchUpdate = false;
        this.size = 0L;
        this.patchUpdateSize = 0L;
        this.enableUpdateByAppstore = true;
    }

    AppInfo(JSONObject json, int localVersionCode) {
        this.isNewVersion = false;
        this.isForcedUpdate = false;
        this.versionCode = 0;
        this.isPatchUpdate = false;
        this.size = 0L;
        this.patchUpdateSize = 0L;
        this.enableUpdateByAppstore = true;
        this.versionCode = json.optInt("code");
        this.isNewVersion = json.optInt("ismust") == 1 ? true : false;
        if (localVersionCode < versionCode) {
            this.isNewVersion = true;
        }
        this.isForcedUpdate = json.optInt("forceUpdate") == 1 ? true : false;
        this.isPatchUpdate = json.optBoolean("save_update");
        this.size = json.optLong("apk_size");
        this.patchUpdateSize = json.optLong("save_size");
        this.updateTitle = json.optString("tips_title");
        this.updateTitle = "新的版本";
        this.updateComment = json.optString("updateContent");
        this.versioName = json.optString("versions");
        this.downloadUrl = json.optString("downUrl");
        this.appStoreDownloadUrl = json.optString("appstore_download_url", (String)null);
        this.appstoreChannelName = json.optString("appstore_channel_name", (String)null);
        if(TextUtils.isEmpty(this.appStoreDownloadUrl)) {
            this.appStoreDownloadUrl = "http://openbox.mobilem.360.cn/channel/getUrl?src=310000&app=zs";
        }

        if(TextUtils.isEmpty(this.appstoreChannelName)) {
            this.appstoreChannelName = "310000";
        }

        this.enableUpdateByAppstore = json.optBoolean("enable_update_by_appstore", true);
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dst, int flag) {
        dst.writeInt(this.isNewVersion?1:0);
        dst.writeInt(this.isForcedUpdate?1:0);
        dst.writeInt(this.versionCode);
        dst.writeInt(this.isPatchUpdate ? 1 : 0);
        dst.writeLong(this.size);
        dst.writeLong(this.patchUpdateSize);
        dst.writeString(this.updateTitle);
        dst.writeString(this.updateComment);
        dst.writeString(this.versioName);
        dst.writeString(this.downloadUrl);
        dst.writeString(this.packageName);
        dst.writeString(this.appStoreDownloadUrl);
        dst.writeString(this.appstoreChannelName);
        dst.writeInt(this.enableUpdateByAppstore?1:0);
    }

    private AppInfo(Parcel source) {
        this.isNewVersion = false;
        this.isForcedUpdate = false;
        this.versionCode = 0;
        this.isPatchUpdate = false;
        this.size = 0L;
        this.patchUpdateSize = 0L;
        this.enableUpdateByAppstore = true;
        this.isNewVersion = source.readInt() == 1;
        this.isForcedUpdate = source.readInt() == 1;
        this.versionCode = source.readInt();
        this.isPatchUpdate = source.readInt() == 1;
        this.size = source.readLong();
        this.patchUpdateSize = source.readLong();
        this.updateTitle = source.readString();
        this.updateComment = source.readString();
        this.versioName = source.readString();
        this.downloadUrl = source.readString();
        this.packageName = source.readString();
        this.appStoreDownloadUrl = source.readString();
        this.appstoreChannelName = source.readString();
        this.enableUpdateByAppstore = source.readInt() == 1;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("AppInfo[");
        sb.append("\n\tisNewVersion:").append(this.isNewVersion);
        sb.append("\n\t,isForcedUpdate:").append(this.isForcedUpdate);
        sb.append("\n\t,code:").append(this.versionCode);
        sb.append("\n\t,isPatchUpdate:").append(this.isPatchUpdate);
        sb.append("\n\t,size:").append(this.size);
        sb.append("\n\t,patchUpdateSize:").append(this.patchUpdateSize);
        sb.append("\n\t,updateTitle:").append(this.updateTitle);
        sb.append("\n\t,updateContent:").append(this.updateComment);
        sb.append("\n\t,versioName:").append(this.versioName);
        sb.append("\n\t,downUrl:").append(this.downloadUrl);
        sb.append("\n\t,packageName:").append(this.packageName);
        sb.append("\n\t,appStoreDownloadUrl:").append(this.appStoreDownloadUrl);
        sb.append("\n\t,appstoreChannelName:").append(this.appstoreChannelName);
        sb.append("\n\t,enableUpdateByAppstore:").append(this.enableUpdateByAppstore);
        sb.append("\n\t]");
        return sb.toString();
    }
}
